//
//  CCFirstScroll.m
//  CwinCar
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCFirstScroll.h"

@implementation CCFirstScroll

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self creatScroll];
        
    }
    return self;
}
-(void)creatScroll
{
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, UIView_Height )];
    _scroll.pagingEnabled = YES;
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.userInteractionEnabled = YES;
    _scroll.delegate = self;
    
    //设置定时器
    _time = [NSTimer scheduledTimerWithTimeInterval:5. target:self selector:@selector(timer) userInfo:nil repeats:YES];
    
    //创建scrollView上的小圆点
    _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, UIView_Height - 30, UIView_Width, 30 )];
    [_page setCurrentPageIndicatorTintColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];
    [_page addTarget:self action:@selector(pageClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_scroll];
    [self addSubview:_page];
    
    
    
}
- (void)setData:(NSMutableArray *)array
{
    _pictureNumber = array.count;
    _page.numberOfPages = array.count;
    
    _scroll.contentSize = CGSizeMake(UIView_Width* (array.count + 1), 0);
    NSString *fisrt = [array firstObject];
    NSString *last = [array lastObject];
    [array insertObject:last atIndex:0];
    [array addObject:fisrt];
    for (int i = 0; i < array.count; i ++) {
        UIImageView *imgeView = [[UIImageView alloc]initWithFrame:CGRectMake((i - 1) * UIView_Width , 0, UIView_Width, UIView_Height)];
        [imgeView setUserInteractionEnabled:YES];
        CCFirstPageModel *model = [array objectAtIndex:i];
        [imgeView sd_setImageWithURL:[NSURL URLWithString:model.imgURL]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        //        UILabel *numLabel = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width * i + 10, mainScree_Height / 4.446, mainScreen_Width / 12.5, mainScree_Height / 22.233)];
        //        numLabel.text = [NSString stringWithFormat:@"%d/5",i + 1];
        //        [numLabel setTextColor:[UIColor whiteColor]];
        //        numLabel.font = [UIFont systemFontOfSize:12.];
        //        [_scroll addSubview:numLabel];
        [_scroll addSubview:imgeView];
        
        
    }
    
}
-(void)pageClick:(UIPageControl *)page
{
    [_scroll setContentOffset:CGPointMake(page.currentPage * UIView_Width, 0)];
}
-(void)timer
{
    if (_scroll.contentOffset.x == _scroll.frame.size.width*_pictureNumber) {
        
        //使计时器暂停
        NSDate *pause = [NSDate dateWithTimeIntervalSinceNow:0] ;
        NSDate *previousFireDate = [_time fireDate] ;
        [_time setFireDate:[NSDate distantFuture]];
        
        //清除最后跳转图片的多余的一倍的时间
        _pauseTime = [NSTimer scheduledTimerWithTimeInterval:.00001 target:self selector:@selector(pause) userInfo:nil repeats:NO];
        
        // 重运行定时器
        float pauseTime = -.1*[pause timeIntervalSinceNow];
        [_time setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
        
    }
    
    
    [_scroll setContentOffset:CGPointMake(_scroll.contentOffset.x+_scroll.frame.size.width, 0) animated:YES];
}
//跳过最后一张假图片的方法
-(void)pause
{
    
    [_scroll setContentOffset:CGPointMake(0, 0) animated:NO];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //手动开始拖拽的时候停止定时器
    [_time setFireDate:[NSDate distantFuture]];
}
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    //保证从滑动结束之后的位置重新计时
    [_time setFireDate:[NSDate dateWithTimeIntervalSinceNow:3.]];
    
}
#pragma mark-scrollView滚动方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    //保证小圆点位置对应
    if (_scroll.contentOffset.x == _scroll.frame.size.width*_pictureNumber) {
        _page.currentPage = 0;
        
    }
    if (_scroll.contentOffset.x != _scroll.frame.size.width*_pictureNumber) {
        
        _page.currentPage = _scroll.contentOffset.x/_scroll.frame.size.width;
    }
    
    //保证滑动无间隙
    if (_scroll.contentOffset.x < _scroll.frame.size.width * 0) {
        [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width*_pictureNumber, 0) animated:NO];
    }
    if (_scroll.contentOffset.x > _scroll.frame.size.width * _pictureNumber) {
        [_scroll setContentOffset:CGPointMake(_scroll.frame.size.width*0, 0) animated:NO];
    }
    
}
@end
